﻿'use strict';

var myAPP = angular.module('myAPP', ['ngRoute', 'ui.bootstrap', 'ng-bootstrap-datepicker', 'ngAnimate', 'ngCookies', 'ngMessages']);

myAPP.config(['$routeProvider',
    function (
        $routeProvider
    ) {
        $routeProvider.
              when('/home', {
                  templateUrl: '/UI/Member/Home.aspx',
                  //templateUrl: '/PaydSystem/UI/Member/Home.aspx',
                  controller: 'HomeController'
              }).
              when('/login', {
                  templateUrl: '/UI/Member/login.aspx',
                  //templateUrl: '/PaydSystem/UI/Member/login.aspx',
                  controller: 'LoginController'
              }).
              when('/Profile', {
                  templateUrl: '/UI/Member/Profile.aspx',
                  //templateUrl: '/PaydSystem/UI/Member/Profile.aspx',
                  controller: 'ProfileController'
              }).
              when('/Activate/:memberId', {
                  templateUrl: '/UI/Member/Activate.aspx',
                  //templateUrl: '/PaydSystem/UI/Member/Activate.aspx',
                  controller: 'ActivateController'
              }).
              when('/ChangePassword/:memberId', {
                  templateUrl: '/UI/Member/ChangePassword.aspx',
                  //templateUrl: '/PaydSystem/UI/Member/ChangePassword.aspx',
                  controller: 'ChangePasswordController'
              }).
              when('/register', {
                  templateUrl: '/UI/Member/Register.aspx',
                  //templateUrl: '/PaydSystem/UI/Member/Register.aspx',
                  controller: 'RegisterController'
              }).
              when('/Contact', {
                  templateUrl: '/UI/Company/Contact.aspx',
                  //templateUrl: '/PaydSystem/UI/Company/Contact.aspx',
                  controller: 'ContactController'
              }).
              when('/About', {
                  templateUrl: '/UI/Project/About.aspx',
                  //templateUrl: '/PaydSystem/UI/Project/About.aspx',
                  controller: 'AboutController'
              }).
              when('/Dictionary', {
                  templateUrl: '/UI/Project/Dictionary.aspx',
                  //templateUrl: '/PaydSystem/UI/Project/Dictionary.aspx',
                  controller: 'DictionaryController'
              }).
              when('/Question', {
                  templateUrl: '/UI/Project/Question.aspx',
                  //templateUrl: '/PaydSystem/UI/Project/Question.aspx',
                  controller: 'QuestionController'
              }).
              otherwise({
                  redirectTo: '/home'
              });
    } ])
    .run(function ($rootScope, $location, $http) {
        $rootScope.$on("$routeChangeStart", function (event, next, current) {
            $rootScope.authenticated = false;

            $http({
                url: "../../PaydSw.asmx/CheckUserExistLogin",
                dataType: 'json',
                method: 'POST',
                data: '',
                headers: {
                    "Content-Type": "application/json"
                }
            }).success(function (response) {
                var data = JSON.parse(response.d);

                if (data.Result != 0) {
                    $rootScope.authenticated = true;
                    //$rootScope.memberId = data.RecData.MemberId;
                    //                  $rootScope.name = results.name;
                    //                  $rootScope.email = results.email;
                    var nextUrl = next.$$route.originalPath;
                    if (nextUrl == '/login' || nextUrl == '/register') {
                        $location.path('/Profile');
                                        }
                    //$location.path('/Profile');
                }
                else {
                    $rootScope.authenticated = false;
                    if (next.$$route != undefined) {
                        var nextUrl = next.$$route.originalPath;
                        if (nextUrl == '/Profile') {
                            $location.path("/login");
                        }
                    }
//                    else {
//                        $location.path("/home");
//                    }
                }
                //$rootScope.test = 3;
                
            })
            .error(function (error) {
                var dataError = JSON.parse(error.responseText);
                var ErrorMessage = 'error at CheckExistLogin ' + dataError.ExceptionType + ' ' + dataError.Message + ' ' + dataError.StackTrace;
                alert(ErrorMessage);
                $location.path('/home');
            });

        });
    });     

    